using Sandia.MVCFramework.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sandia.MVCFramework.Request
{
    /// <summary>
    /// Abstract implementation of IRequest that provides
    /// standard functionality for setting up a request
    /// and its parameters
    /// </summary>
    public abstract class AbstractStandardRequest: IRequest
    {
        /// <summary>
        /// Storage for request parameters
        /// </summary>
        protected Dictionary<string, IRequestParameter> parameters;

        /// <summary>
        /// Identifier for this request
        /// </summary>
        protected string requestID;

        /// <summary>
        /// Identifier for the requester
        /// </summary>
        protected string requesterID;

        /// <summary>
        /// Default constructor; initializes parameters
        /// </summary>
        public AbstractStandardRequest()
        {
            this.requestID = System.Guid.NewGuid().ToString();
            this.parameters = new Dictionary<string, IRequestParameter>();
            this.parameters.Add(ShowProgressBarRequestParameter.NAME, new ShowProgressBarRequestParameter(""));
            this.initParameters();
        }

        /// <summary>
        /// Initializes parameters
        /// </summary>
        protected abstract void initParameters();

        #region IRequest Members

        /// <summary>
        /// Gets/sets the request parameter associated with the given parameter name
        /// </summary>
        /// <param name="parameterName">Parameter name to get/set parameter for</param>
        /// <returns>The request parameter for the given parameter name</returns>
        public IRequestParameter this[string parameterName]
        {
            get { return this.parameters[parameterName]; }
            set
            {
                // Restict setter to only parameter names
                // made available by initParameters
                if (this.parameters.ContainsKey(parameterName))
                {
                    this.parameters[parameterName] = value;
                }
                else
                {
                    LoggingTools.SendToLog(
                        "Invalid parameter name for this implementation of IRequestParameter",
                        LoggingCategories.ERROR,
                        100);
                }
            }
        }

        /// <summary>
        /// A mapping of parameter names to request parameters
        /// </summary>
        public Dictionary<string, IRequestParameter> Parameters
        {
            get { return this.parameters; }
        }

        /// <summary>
        /// Identifier for this request
        /// </summary>
        public string RequestID
        {
            get { return this.requestID; }
        }

        /// <summary>
        /// Identifier for the requester
        /// </summary>
        public string RequesterID
        {
            get { return this.requesterID; }
            set { this.requesterID = value; }
        }

        #endregion
    }
}
